package cn.crm.spring.service;


import cn.crm.spring.pojo.Right;
import cn.crm.spring.pojo.Role;
import cn.crm.spring.repository.RightRepository;
import cn.crm.spring.repository.RoleRepository;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("roleService")
public class IRoleServiceImpl implements IRoleService {

    @Resource
    RoleRepository roleRepository;
    @Resource
    RightRepository rightRepository;

    @Override
    @Cacheable(value = "roles", keyGenerator = "keyGenerator")
    public List<Role> findAllRoles() {
        return roleRepository.findAll();
    }

    @Override
    public List<Right> findAllRights() {
        return rightRepository.findAll();
    }

    @Override
    public List<Right> findRightsByRole(Role role) {
        return rightRepository.findRightsByRolesOrderByRightCode(role);
    }

    @Override
    public Role saveRole(Role role) {
        return roleRepository.save(role);
    }

    @Override
    public void deleteRoleById(Integer roleId) {
        roleRepository.deleteById(roleId);
    }

    @Override
    public void deleteRole(Role role) {
        roleRepository.delete(role);
    }

    @Override
    public Role getRole(Integer roleId) {
        return roleRepository.findById(roleId).get();
    }

    @Override
    public Page<Role> findRole(String roleName, Pageable pageable) {
        return roleRepository.findRolesByRoleNameLike("%" + roleName + "%", pageable);
    }
}
